package com.neuedu.dao.Imp;

import cn.hutool.core.util.StrUtil;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.db.handler.BeanHandler;
import com.neuedu.dao.ShopDao;
import com.neuedu.pojo.Shop;
import com.neuedu.utils.JDBCUtil;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class ShopDaoImp implements ShopDao {
    private DataSource dataSource = JDBCUtil.getDruidDataSource();

    @Override
    public Integer addShop(String shopName, String shopAddress, String shopExplain, String shopImg, String shopCategoryId, String startPrice, String deliveryPrice, String remarks,String status, String startTime, String endTime, String recommend, String businessId) throws Exception{
        return Db.use(dataSource).insert(
                Entity.create("shop")
                        .set("shop_name",shopName)
                        .set("shop_address",shopAddress)
                        .set("shop_explain",shopExplain)
                        .set("shop_img",shopImg)
                        .set("shop_category_id",shopCategoryId)
                        .set("start_price",startPrice)
                        .set("delivery_price",deliveryPrice)
                        .set("remarks",remarks)
                        .set("status",status)
                        .set("start_time",startTime)
                        .set("end_time",endTime)
                        .set("recommend",recommend)
                        .set("business_id",businessId)
        );
    }

    // 根据商家id分页查询
    @Override
    public List<Shop> getShop(String currentPage, String pageSize, String businessId) throws Exception{
        String spl = "select * from shop where business_id = " + businessId;
        Integer page = 1;
        Integer size = 2;
        if(StrUtil.isNotBlank(currentPage)){
            page = Integer.valueOf(currentPage);
        }
        if(StrUtil.isNotBlank(pageSize)){
            size = Integer.parseInt(pageSize);
        }
        page = (page - 1) * size;
        spl += " limit " + page + " , " + size;
        return Db.use(dataSource).query(spl,Shop.class);
    }

    @Override
    public Long shopLong(String businessId) throws Exception{
        return Db.use(dataSource).count(Entity.create("shop").set("business_id",businessId));
    }


    // 条件和分页查询所有店铺信息


    @Override
    public Long shopLongs() throws Exception{
        return  Db.use(dataSource).count(Entity.create("shop"));
    }

    @Override
    public List<Shop> getsShop(String currentPage, String pageSize, String shopName, String status) throws Exception{
        String sql = "select * from shop ";

        // 定义一个map集合，将搜索条件对应的列名作为map的key，搜索条件的值作为map的value
        Map<String,String> map = new HashMap<>();
        // 判断传递的参数
        if(StrUtil.isNotBlank(shopName)){
            map.put("shopName",shopName);
        }
        if(StrUtil.isNotBlank(status)){
            map.put("status",status);

        }
        // 这里可以直接判断map是否为空
        if(!map.isEmpty()){
            sql += " where ";
            // 判断成立，说明map中有搜索的条件数据，这时只要遍历map，去给sql拼接条件即可
            Set<Map.Entry<String,String>> entries = map.entrySet();
            for (Map.Entry<String, String> entry : entries) {
                sql += entry.getKey() + " like" + " '%" + entry.getValue() + "%' or ";
            }
            // sql的最后多or关键字 ，需要单独处理"select * from business where account like %?% or telephone like %?% or contact like %?% or "
            sql = sql.substring(0,sql.lastIndexOf("or"));
        }
        // "select * from business where account like %?% or telephone like %?% or contact like %?% limit 起始位置 , 条数"
        // 判断分页的参数、
        Integer page = 1;
        Integer size = 2;
        if(StrUtil.isNotBlank(currentPage)){
            page = Integer.valueOf(currentPage);
        }
        if(StrUtil.isNotBlank(pageSize)){
            size = Integer.parseInt(pageSize);
        }
        sql += " limit " + (page - 1) * size + " , " + size;
        System.out.println(sql);
        return Db.use(dataSource).query(sql, Shop.class);
    }

    @Override
    public Integer updateShop(String shopName, String shopAddress, String shopExplain, String shopImg, String shopCategoryId, String startPrice, String deliveryPrice, String remarks, String startTime, String endTime, String status,String shopId) throws Exception {
        return Db.use(dataSource).update(
                Entity.create()
                        .set("shop_name",shopName)
                        .set("shop_address",shopAddress)
                        .set("shop_explain",shopExplain)
                        .set("shop_img",shopImg)
                        .set("shop_category_id",shopCategoryId)
                        .set("start_price",startPrice)
                        .set("delivery_price",deliveryPrice)
                        .set("remarks",remarks)
                        .set("start_time",startTime)
                        .set("end_time",endTime)
                        .set("status",status),
                Entity.create("shop")
                        .set("shop_id",shopId)
        );
    }

    @Override
    public Integer updateStatus(String status,String shopName) throws Exception{
        return Db.use(dataSource).update(
                Entity.create()
                        .set("status",status),
                Entity.create("shop")
                        .set("shop_name",shopName)
        );
    }

    @Override
    public Integer delShop(String shopId) throws Exception {
        return Db.use(dataSource).del(Entity.create("shop").set("shop_id",shopId));
    }

    @Override
    public Shop getShopName(String shopId) throws Exception {
        return Db.use(dataSource).query("select shop_name from shop where shop_id = ?", BeanHandler.create(Shop.class),shopId);
    }
}
